//
//  CXHGroMoreSplashAdapter.m
//  CXHAdSDK-GroMore
//
//  Created by 陈坤 on 2024/10/15.
//

#import "CXHGroMoreSplashAdapter.h"
#import <CXHAdSDK-Core/CXHAdSDKSplashAd.h>

@interface CXHGroMoreSplashAdapter ()<CXHAdSDKSplashAdDelegate>

@property (nonatomic, strong) CXHAdSDKSplashAd *splashAd;

@end

@implementation CXHGroMoreSplashAdapter

- (BUMMediatedAdStatus)mediatedAdStatus {
    return BUMMediatedAdStatusNormal;
}

- (void)loadSplashAdWithSlotID:(nonnull NSString *)slotID andParameter:(nonnull NSDictionary *)parameter {
    if (self.splashAd) {
        self.splashAd.delegate = nil;
        self.splashAd = nil;
    }
    UIView *customBottomView = parameter[BUMAdLoadingParamSPCustomBottomView];
    
    self.splashAd = [[CXHAdSDKSplashAd alloc] init];
    self.splashAd.posId = slotID;
    self.splashAd.delegate = self;
    self.splashAd.bottomView = customBottomView;
    [self.splashAd loadSplashAd];
}

- (void)showSplashAdInWindow:(nonnull UIWindow *)window parameter:(nonnull NSDictionary *)parameter {
    [self.splashAd showSplashAdInWindow:window];
    self.splashAd.controller = window.rootViewController;
    // 模拟广告展示回调
    [self.bridge splashAdWillVisible:self];
}

- (void)dismissSplashAd {
//    [self.splashView removeFromSuperview];
//    [self.customBottomView removeFromSuperview];
}

- (void)didReceiveBidResult:(BUMMediaBidResult *)result {
    // 在此处理Client Bidding的结果回调
    if (result.win) {
        NSInteger winPrice = self.splashAd.getBidPrice;
        if (result.winnerPrice) {
            winPrice = result.winnerPrice;
        }
        [self.splashAd sendWinNotice:winPrice];
    } else {
        if (result.winnerPrice) {
            [self.splashAd sendLossNotice:result.winnerPrice];
        }
    }
}

#pragma mark - CXHAdSDKSplashAdDelegate

/**
 开屏广告点击回调, 可能会打开 AppStore, WebView, ThirdApp etc.

 @param splashAd 开屏广告
 @param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
 */
- (void)CXH_splashAdDidClick:(CXHAdSDKSplashAd *)splashAd loadingPageURL:(NSString *)loadingPageURL{
    
    [self.bridge splashAdDidClick:self];
}

/**
 开屏广告关闭回调, 触发条件: 点击广告, 点击跳过, 超时关闭

 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidClose:(CXHAdSDKSplashAd *)splashAd{
    
    [self.bridge splashAdDidClose:self];
}

/**
 开屏广告展示回调

 @param splashAd 开屏对象
 */
- (void)CXH_splashAdExposured:(CXHAdSDKSplashAd *)splashAd{
    [self.bridge splashAdWillVisible:self];
}

/**
 开屏广告加载成功
 
 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidLoad:(CXHAdSDKSplashAd *)splashAd{
    [self.bridge splashAd:self didLoadWithExt:@{
        BUMMediaAdLoadingExtECPM : [NSString stringWithFormat:@"%ld", self.splashAd.getBidPrice],
    }];
}

/**
 开屏广告加载失败
 
 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidFailToLoad:(CXHAdSDKSplashAd *)splashAd error:(NSError *)error{
    
    [self.bridge splashAd:self didLoadFailWithError:error ext:@{}];
}

/**
 *  开屏广告成功展示
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdSuccessPresentScreen:(CXHAdSDKSplashAd *)splashAd{
    
}

/**
 *  开屏广告展示失败
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdFailToPresent:(CXHAdSDKSplashAd *)splashAd withError:(NSError *)error{
    [self.bridge splashAdDidShowFailed:self error:error];
}

/**
 *  开屏广告关闭落地页
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdCloseLandingPage:(CXHAdSDKSplashAd *)splashAd{
}

@end
